//
//  MGSection.m
//  Express
//
//  Created by lhongl on 2025/9/25.
//  Copyright © 2025 QM. All rights reserved.
//

#import "MGSection.h"
#import "MGTableViewManage.h"
#import "MGItem.h"
@interface MGSection ()

@property (nonatomic, strong) NSMutableArray <MGItem *> *dataArray;

@property (nonatomic, copy) NSString *sectionHeaderIdent; //header的ident

@property (nonatomic, copy) NSString *sectionFooterIdent; //footer的ident

@end

@implementation MGSection

+ (instancetype)section{
    return [[self alloc] init];
}

- (NSInteger)section{
    return [self.tableManage.sections indexOfObject:self];
}

- (NSArray <MGItem *> *)itemList{
    return _dataArray;
}

- (NSString *)footerIdentifier{
    return self.sectionFooterIdent;
}

- (NSString *)headerIdentifier{
    return self.sectionHeaderIdent;
}

- (void)dequeueReusableHeaderClass:(nullable Class)headerClass footerClass:(nullable Class)footerClass{
    if (headerClass) {
        NSString *headerIdentifier = NSStringFromClass(headerClass);
        self.sectionHeaderIdent = headerIdentifier;
    }
    if (footerClass) {
        NSString *footerIdentifier = NSStringFromClass(footerClass);
        self.sectionFooterIdent = footerIdentifier;
    }
}

#pragma mark - 添加item
- (void)addItem:(MGItem *)item {
    if (!item) {
#ifdef DEBUG
        NSAssert(item, @"[MGTableView_DEBUG] item not Null");
#endif
        return;
    }
    item.section = self;
    [self.dataArray addObject:item];
}

- (void)addItemWithItems:(NSArray <MGItem *>*)items {
    if (items.count == 0) {
#ifdef DEBUG
        NSAssert(items, @"[MGTableView_DEBUG] items not Null");
#endif
        return;
    }
    [items enumerateObjectsUsingBlock:^(MGItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.section = self;
        [self.dataArray addObject:obj];
    }];
}

- (void)insertItem:(MGItem *)item atIndex:(NSUInteger)idx {
    if (!item) {
#ifdef DEBUG
        NSAssert(item, @"[MGTableView_DEBUG] item Not Null");
#endif
        return;
    }

    if (self.dataArray.count < idx) {
#ifdef DEBUG
        NSString *des = [NSString stringWithFormat:@"[MGTableView_DEBUG]: index %ld beyond bounds [0 .. %ld]",idx,self.itemList.count];
        NSAssert(NO, des);
#endif
        return;
    }
    item.section = self;
    [self.dataArray insertObject:item atIndex:idx];
}

- (void)insertItems:(NSArray <MGItem *> *)items atIndex:(NSUInteger)idx {
    if (self.dataArray.count < idx) {
#ifdef DEBUG
        NSString *des = [NSString stringWithFormat:@"[MGTableView_DEBUG]: index %ld beyond bounds [0 .. %ld]",idx,self.itemList.count];
        NSAssert(NO, des);
#endif
        return;
    }
    NSMutableArray <MGItem *>*newItems = [NSMutableArray array];
    [items enumerateObjectsUsingBlock:^(MGItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (!obj) {
            return;
        }
        obj.section = self;
        [newItems addObject:obj];
    }];
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(idx, newItems.count)];
    [self.dataArray insertObjects:newItems atIndexes:indexSet];
}

#pragma mark - 删除
- (void)removeAllItem {
    [self.dataArray removeAllObjects];
}

- (void)removeItemWithIndex:(NSInteger)index {
    if (self.dataArray.count <= index) {
#ifdef DEBUG
        NSString *des = [NSString stringWithFormat:@"[MGTableView_DEBUG]: index %ld beyond bounds [0 .. %ld]",index,self.itemList.count - 1];
        NSAssert(NO, des);
#endif
        return;
    }
    [self.dataArray removeObjectAtIndex:index];
}

- (void)removeItemWithItems:(NSArray <MGItem *>*)items {
    if (items.count == 0) {
        return;
    }
    [self.dataArray removeObjectsInArray:items];
}

- (void)removeItem:(MGItem *)item {
    if (!item) {
#ifdef DEBUG
        NSAssert(item, @"[MGTableView_DEBUG] item not Null");
#endif
        return;
    }
    [self.dataArray removeObject:item];
}

#pragma mark 替换
- (void)replaceItemWithIndex:(NSUInteger)index item:(MGItem *)item {
    if (!item) {
#ifdef DEBUG
        NSAssert(item, @"[MGTableView_DEBUG] item Not problem");
#endif
        return;
    }
    if (self.itemList.count <= index) {
#ifdef DEBUG
        NSString *des = [NSString stringWithFormat:@"[MGTableView_DEBUG]: index %ld beyond bounds [0 .. %ld]",index,self.itemList.count - 1];
        NSAssert(NO, des);
#endif
        return;
    }
    item.section = self;
    [self.dataArray replaceObjectAtIndex:index withObject:item];
}

#pragma mark 懒加载
- (NSMutableArray <MGItem *>*)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
